/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.fresco.samples.showcase.slice;
import com.facebook.fresco.samples.showcase.ResourceTable;
import com.facebook.fresco.samples.showcase.drawee.*;
import com.facebook.fresco.samples.showcase.imageformat.ImageFormatGifAbility;
import com.facebook.fresco.samples.showcase.imagepipeline.ImagePipelineRegionDecodingAbility;
import com.facebook.fresco.samples.showcase.imagepipeline.ImagePipelineResizingAbility;
import com.facebook.fresco.samples.showcase.list.ListViewAbility;
import com.oszc.bbhmlibrary.utils.LogUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.app.Context;

public class MainAbilitySlice extends AbilitySlice {
    private final String TAG = MainAbilitySlice.class.getSimpleName();
    private Component mBtSimpleDrawee, mBtDraweeMediaPicker,mBtDraweeHierarchy,mBtDraweeScaleType
            ,mBtDraweeRotation,mBtRoundCorner,mBtnMultiUri,mBtGif,mBtList;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        findviews();
        setClickListeners();
    }

    private void findviews(){

        mBtList = findComponentById(ResourceTable.Id_bt_list);
        mBtSimpleDrawee = findComponentById(ResourceTable.Id_bt_simple_drawee);
        mBtDraweeMediaPicker = findComponentById(ResourceTable.Id_bt_drawee_media_picker);
        mBtDraweeHierarchy = findComponentById(ResourceTable.Id_bt_drawee_hierarchy);
        mBtDraweeScaleType = findComponentById(ResourceTable.Id_bt_drawee_scaleType);
        mBtDraweeRotation = findComponentById(ResourceTable.Id_bt_drawee_rotation);
        mBtRoundCorner = findComponentById(ResourceTable.Id_bt_drawee_RoundCorner);
        mBtnMultiUri = findComponentById(ResourceTable.Id_bt_multi_uri);
        findComponentById(ResourceTable.Id_bt_imagepipeline_region_decoding).setClickedListener(view->startAbility(this, ImagePipelineRegionDecodingAbility.class));
        findComponentById(ResourceTable.Id_bt_imagepipeline_resizing).setClickedListener(view->startAbility(this, ImagePipelineResizingAbility.class));
        mBtGif= findComponentById(ResourceTable.Id_bt_gif);
    }

    private void setClickListeners(){
        LogUtil.error(TAG,getDataDir().getAbsolutePath());
        mBtSimpleDrawee.setClickedListener(view->startAbility(this, DraweeSimpleAbility.class));
        mBtDraweeMediaPicker.setClickedListener(view->startAbility( this,DraweeMediaPickerAbility.class));
        mBtDraweeHierarchy.setClickedListener(view->startAbility(this, DraweeHierarchyAbility.class));
        mBtDraweeScaleType.setClickedListener(view->startAbility(this, DraweeScaleTypeAbility.class));
        mBtDraweeRotation.setClickedListener(view->startAbility(this, DraweeRotationAbility.class));
        mBtRoundCorner.setClickedListener(view->startAbility(this, DraweeRoundedCornerAbility.class));
        mBtnMultiUri.setClickedListener(view->startAbility(this, MultiUriAbility.class));
        mBtGif.setClickedListener(view->startAbility(this, ImageFormatGifAbility.class));
        mBtList.setClickedListener(view->startAbility(this, ListViewAbility.class));
    }

    public void startAbility(Context context, Class clazz){
        Intent.OperationBuilder builder = new Intent.OperationBuilder();
        Operation operation= builder
                .withDeviceId("")
                .withBundleName(context.getBundleName())
                .withAbilityName(clazz.getCanonicalName()).build();
        Intent intent = new Intent();
        intent.setOperation(operation);
        startAbility(intent);
    }
}
